/*
 * CCreateWorld.cpp
 *
 *  Created on: 28.06.2009
 *      Author: Oli
 */
#include "CCreateWorld.h"
#include <Ogre.h>
//#include "../src/ErrorOutputs.h"
#include "../Graphics/CGraphicEngine.h"
#include <list>
#include <SDL.h>

CCreateWorld::CCreateWorld()
{
}

CCreateWorld::~CCreateWorld()
{
}

void CCreateWorld::SpawnPrototypeWorld(ConfigContainer *configContainer)
{
	int iExitCode = 0;
	try
	{

    	writeDebugOutput("I am alive! :)");
		CLivingObject test;
		std::list<CLivingObject> lstLivingObj; // the first item in the list will be the player object

    	writeDebugOutput("main(): Creating player object.");
		lstLivingObj.push_back(test ); // we created it local. all other living object should be created on the other clients and pushed back in this list.

    	writeDebugOutput("main(): Creating graphic engine object.");
        CGraphicEngine ogreEngine;

		writeDebugOutput("Inititialise SDL");
		if(SDL_Init(SDL_INIT_VIDEO) != 0) // Video initialises the event stuff too! (call initsubsystem for additional, well... subsystems)
		{
			writeDebugOutput("SDL failed to inititialise - aborting!");
			return;
		}

		if(!ogreEngine.start(&lstLivingObj, *configContainer))
        {
        	writeDebugOutput("main: start() returned false. Exiting program properly.");
			iExitCode = 1;
        }
    }
    catch(Exception e)
    {
    	writeExceptionOutput(e.getFullDescription().c_str());
		writeDebugOutput("Shutting down SDL");
		SDL_Quit();
    }
	
	writeDebugOutput("Shutting down SDL");
	SDL_Quit();

    writeDebugOutput("I am going to die! :(");

	//just wait till a button is pressed - not very elegant...
	//we need to do this, since the console closes when the program exits
	//TODO: maybe we should write a logfile...
    char txt[1024];
    sprintf(txt,"Program has finished with exit code %d. Press any key to continue.", iExitCode);
    writeDebugOutput(txt);

//#ifdef _DEBUG
//	#ifndef __gnu_linux__
//		std::getchar();
//	#else
//		std::getwchar();
//	#endif
//#endif
    writeDebugOutput("Goodbye!");
}

